package com.docker.sys.menu;

import com.docker.core.mvc.service.BaseService;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Okv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import kit.docker.convert.RecordKit;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author ldl
 * @Create 2020/09/18 018 10:01
 */
public class MenuService extends BaseService {

    @Inject
    private MenuDao menuDao;

    public List<Record> tree(Okv cond) {
        return buildTree(list(cond));
    }

    public List<Record> list(Okv cond) {
        return menuDao.list(cond);
    }

    public List<Record> buildTree(List<Record> menus) {
        List<Record> menuTree = new ArrayList<>();
        for (Record menu : menus) {
            if (StrKit.isBlank(menu.getStr("menu_pid")) && menu.getInt("menu_level") == 1) { //一级菜单
                getSonMenus(menu, menus);
                menuTree.add(menu);
            }
        }
        return menuTree;
    }

    private void getSonMenus(Record parnetMenu, List<Record> menus) {
        List<Record> sonList = new ArrayList<>();
        for (Record menu : menus) {
            if (StrKit.notBlank(menu.getStr("menu_pid")) && menu.getStr("menu_pid").equals(parnetMenu.getStr("menu_id"))) {
                getSonMenus(menu, menus);
                sonList.add(menu);
            }
        }
        parnetMenu.set("child_menu", sonList);
    }

    public boolean save(MenuBean bean, String userId) {
        bean.setMenu_level(1);
        if (StrKit.notBlank(bean.getMenu_pid())) {
            int pLevel = menuDao.pLevel(bean.getMenu_pid());
            if (pLevel > 2) {
                throwError("最多添加三级菜单");
            }
            bean.setMenu_level(pLevel + 1);
        }
        bean.setMenu_id(getUUID32());
        bean.setStatus_flag("1");
        bean.setCreate_time(getNowDateTime());
        bean.setCreate_oper_id(userId);
        bean.setUpdate_time(bean.getCreate_time());
        bean.setUpdate_oper_id(userId);
        return menuDao.save(RecordKit.objectToRecord(bean));
    }

    public boolean update(MenuBean bean, String userId) {
        Record menu = menuDao.info(bean.getMenu_id());
        if (!StrKit.notNull(menu)) {
            throwError("菜单不存在");
        }
        menu.set("menu_name", bean.getMenu_name());
        menu.set("menu_url", bean.getMenu_url());
        menu.set("menu_desc", bean.getMenu_desc());
        menu.set("menu_target", bean.getMenu_target());
        menu.set("menu_css", bean.getMenu_css());
        menu.set("menu_icon", bean.getMenu_icon());
        menu.set("menu_order", bean.getMenu_order());
        menu.set("update_time", getNowDateTime());
        menu.set("update_oper_id", userId);
        return menuDao.update(menu);
    }

    public boolean enable(String menuId, String userId) {
        Record menu = menuDao.info(menuId);
        if (!StrKit.notNull(menu)) {
            throwError("菜单不存在");
        }
        if ("1".equals(menu.getStr("status_flag"))) {
            throwError("菜单已启用");
        }
        menu.set("status_flag", "1");
        menu.set("update_time", getNowDateTime());
        menu.set("update_oper_id", userId);
        return menuDao.update(menu);
    }

    public boolean disable(String menuId, String userId) {
        Record menu = menuDao.info(menuId);
        if (!StrKit.notNull(menu)) {
            throwError("菜单不存在");
        }
        if (!"1".equals(menu.getStr("status_flag"))) {
            throwError("菜单已停用");
        }
        menu.set("status_flag", "0");
        menu.set("update_time", getNowDateTime());
        menu.set("update_oper_id", userId);
        return menuDao.update(menu);
    }

}
